package chapter03_binaryTree;

import javafx.beans.binding.When;
import jdk.internal.dynalink.support.AbstractCallSiteDescriptor;

/**
 * 描述：
 *
 * @author hl
 * @date 2021/5/5 11:23
 */
public class PrintTree {
    public void printTree(Node root){
        if (root == null) {
            return ;
        }
        System.out.println("Binray Tree:");
        process(root, 1, "H", 17);
    }
    public void process(Node node, int height, String to, int len){
        if (node == null) {
            return ;
        }
        process(node.right, height + 1, "v", len);
        String value = to + node.val + to;
        int lenM = value.length();
        int lenL = (len - lenM) / 2;
        int lenR = len - lenM - lenL;
        System.out.println(getSpace(len * height) + getSpace(lenL) + value + getSpace(lenR));
        process(node.left, height + 1, "^", len);
    }

    private String getSpace(int lenL) {
        StringBuilder res = new StringBuilder();
        for (int i = 0; i < lenL; i++) {
            res.append(" ");
        }
        return res.toString();
    }
}
